package gui;

import database.*;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PanelFrakt extends JPanel implements ActionListener{
	private JLabel labelFraktpris, labelFriFrakt, labelNyFraktpris, labelNyFriFrakt, labelEndrePris;
	private JTextField fieldFraktpris, fieldFriFrakt;
	private JButton buttonEndreFrakt, buttonEndreFri, buttonOK, buttonHovedmeny, buttonTilbake;
	private GridBagConstraints constraints;
	public PanelFrakt(){
		this.setLayout(new GridBagLayout());
		this.leggTilKomponenter();
		GUI.getVindu().add(this);
	}
	private void leggTilKomponenter(){
		labelFraktpris = new JLabel("Fraktpris: " + Databasecon.getFraktPris());
		labelFriFrakt = new JLabel("Grense for fri frakt: " + Databasecon.getGratisLeveringBellop());
		Font font = new Font(labelFriFrakt.getFont().getFontName(),labelFriFrakt.getFont().getStyle(),16);
		labelFraktpris.setFont(font);
		labelFriFrakt.setFont(font);
		
		labelNyFraktpris = new JLabel("Fraktpris");
		labelNyFriFrakt = new JLabel("Grense for fri frakt");
		
		labelEndrePris = new JLabel("Endre priser for frakt");
		font = new Font(labelEndrePris.getFont().getFontName(),labelEndrePris.getFont().getStyle(),30);
		labelEndrePris.setFont(font);
		
		fieldFraktpris = new JTextField(20);
		fieldFraktpris.addActionListener(this);
		fieldFriFrakt = new JTextField(20);
		fieldFriFrakt.addActionListener(this);
		
		buttonEndreFrakt = new JButton("Endre");
		buttonEndreFrakt.addActionListener(this);
		buttonEndreFri = new JButton("Endre");
		buttonEndreFri.addActionListener(this);
		buttonOK = new JButton("OK");
		buttonOK.addActionListener(this);
		buttonHovedmeny = new JButton("Tilbake til hovedmeny");
		buttonHovedmeny.addActionListener(this);
		buttonTilbake = new JButton("Tilbake");
		buttonTilbake.addActionListener(this);
		
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(20, 10, 10, 10);
		constraints.gridwidth = 6;
		this.add(labelEndrePris, constraints);
		constraints.gridwidth = 1;
		constraints(3, 2);
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(labelFraktpris, constraints);
		constraints.anchor = GridBagConstraints.CENTER;
		constraints(0, 2);
		this.add(labelNyFraktpris,constraints);
		constraints(1, 2);
		this.add(fieldFraktpris,constraints);
		constraints(2, 2);
		this.add(buttonEndreFrakt,constraints);
		constraints(3, 3);
		this.add(labelFriFrakt,constraints);
		constraints(0, 3);
		this.add(labelNyFriFrakt, constraints);
		constraints(1, 3);
		this.add(fieldFriFrakt, constraints);
		constraints(2, 3);
		this.add(buttonEndreFri, constraints);
		constraints(0, 4);
		this.add(buttonOK, constraints);
		constraints(1, 4);
		this.add(buttonTilbake, constraints);
		constraints(2, 4);
		this.add(buttonHovedmeny, constraints);
		
		
	}
	private void constraints(int x, int y){
		constraints.gridx = x;
		constraints.gridy = y;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonEndreFrakt)
			buttonEndreFrakt_actionPerformed(e);
		else if(e.getSource()==buttonEndreFri)
			buttonEndreFri_actionPerformed(e);
		else if(e.getSource()==buttonOK)
			buttonOK_actionPerformed(e);
		else if(e.getSource()==buttonHovedmeny)
			buttonHovedmeny_actionPerformed(e);
		else if(e.getSource()==buttonTilbake)
			buttonTilbake_actionPerformed(e);
			
		
	}
	private void buttonHovedmeny_actionPerformed(ActionEvent e) {
		this.setVisible(false);
		new PanelHovedmeny();
		
	}
	private void buttonTilbake_actionPerformed(ActionEvent e) {
		this.setVisible(false);
		new PanelAdmin();
		
	}
	private void buttonOK_actionPerformed(ActionEvent e) {
		this.setVisible(false);
		new PanelAdmin();
		
	}
	private void buttonEndreFri_actionPerformed(ActionEvent e) {
		Databasecon.setGratisLeveringBellop(Integer.parseInt(fieldFriFrakt.getText()));
		labelFriFrakt.setText("Grense for fri frakt: " + Databasecon.getGratisLeveringBellop());
		fieldFriFrakt.setText("");
		
	}
	private void buttonEndreFrakt_actionPerformed(ActionEvent e) {
		Databasecon.setFraktPris(Integer.parseInt(fieldFraktpris.getText()));
		labelFraktpris.setText("Fraktpris: " + Databasecon.getFraktPris());
		fieldFraktpris.setText("");
	}
}